$(function () {
    //   获取数据
    // 看文档发起ajax请求
    $.ajax({
        url: bignews.user_detail,
        headers: { 'Authorization': localStorage.getItem('bignews_69_token') },
        dataType: 'json',
        async: false,
        success: function (res) {
            console.log(res);
            let nickname = res.data.nickname
            let email = res.data.email
            let password = res.data.password
            let userPic = res.data.userPic
            let username = res.data.username

            $('#inputEmail1').val(username)
            $('#inputEmail2').val(nickname)
            $('#inputEmail3').val(email)
            $('#user_pic').attr('src', userPic)
            $('#inputEmail4').val(password)

            // 添加事件
            $('.btn-edit').on('click', function (e) {
                e.preventDefault()
                let formdata = new FormData()
                formdata.append('username', username)
                formdata.append('nickname', nickname)
                formdata.append('email', email)
                formdata.append('userPic', userPic)
                formdata.append('password', password)
                $.ajax({
                    type: 'post',
                    url: bignews.user_edit,
                    headers: { 'Authorization': localStorage.getItem('bignews_69_token') },
                    dataType: 'json',
                    processData: false,
                    contentType: false,
                    data: formdata,
                    success: function (res) {
                        console.log(res);
                        alert(res.msg)
                        // top.location.href = './index.html'
                        window.parent.location.reload()


                    }

                })

            })
        }
    })

    $('#exampleInputFile').on('change', function () {
        // 获取到文件
        let myfile = $('#exampleInputFile')[0].files[0]
        if (myfile.type.indexOf('image/') === -1) {
            alert('请选择图片文件')
            return
        }
        let myURL = URL.createObjectURL(myfile)
        $('.user_pic').attr('src', myURL)
    })



})